/**
 * 
 */
package edu.hawaii.its381b.client;

import com.google.gwt.dom.client.AudioElement;
import com.google.gwt.media.client.Audio;
import com.google.gwt.user.client.ui.HTMLPanel;

/**
 * @author  David J. Nickles, PhD
 * @date    May 12, 2012
 * @version 1.0
 */
public class BackgroundAudioPanel extends HTMLPanel {
	
	private Audio audio;
	private AudioElement element;

	/**
	 * @return the audio
	 */
	public Audio getAudio() {
		return audio;
	}

	public BackgroundAudioPanel(String srcUriString) {
		super("");
		
		this.getElement().setId("hidden_audio");
		
        this.audio = Audio.createIfSupported();
        if(audio != null) {
        	element = audio.getAudioElement();
        	element.setSrc(srcUriString);
        	
        	this.audio.setControls(true);
        	this.audio.setLoop(true);

        	this.audio.setVisible(false);
        	this.audio.getElement().setId("bg_audio");
        	this.add(audio);
        
        	this.setVisible(false);
        }
	}
	
	public void load() {
		this.audio.load();
	}
	
	public void play() {
        this.audio.play();
	}
	
	public void pause() {
        this.audio.pause();
	}

	public void play(double rate) {
		this.audio.setPlaybackRate(rate);
		this.audio.play();
	}
	
	public String getAudioElementState() {
		StringBuilder sb = new StringBuilder();
		AudioElement e = audio.getAudioElement();
		sb.append("AudioElement[");
		sb.append("currentSrc=");
		sb.append(e.getCurrentSrc());
		sb.append(",currentTime=");
		sb.append(e.getCurrentTime());
		sb.append(",defaultPlaybackRate=");
		sb.append(e.getDefaultPlaybackRate());
		sb.append(",duration=");
		sb.append(e.getDuration());
		sb.append(",initialTime=");
		sb.append(e.getInitialTime());
		sb.append(",networkState=");
		sb.append(e.getNetworkState());
		sb.append(",playbackRate=");
		sb.append(e.getPlaybackRate());
		sb.append(",preload=");
		sb.append(e.getPreload());
		sb.append(",readyState=");
		sb.append(e.getReadyState());
		sb.append(",src=");
		sb.append(e.getSrc());
		sb.append(",startOffsetTime=");
		sb.append(e.getStartOffsetTime());
		sb.append(",seekable=");
		sb.append(e.getSeekable());
		sb.append(",volume=");
		sb.append(e.getVolume());
		sb.append("]");
		return sb.toString();
	}
	
	public String toString() {
		return this.getAudioElementState();
	}
}
